" Vim syntax file
    " Language:	Markdown
    " Maintainer:	Ben Williams <benw@plasticboy.com>
    " URL:		http://plasticboy.com/markdown-vim-mode/
    " Remark:	Uses HTML syntax file
    " TODO: 	Handle stuff contained within stuff (e.g. headings within blockquotes)

     " “Markdown” is two things:
     " (1) a plain text formatting syntax; and
     " (2) a software tool, written in Perl, that converts the plain text formatting to HTML.

    "\ 规范后的markdown: commonmark
    "\ https://spec.commonmark.org/0.30/
    "\ https://github.com/commonmark/cmark  : powerful C implementation of the CommonMark spec.
        "\ An extended version by github
        "\ https://github.com/github/cmark-gfm

    "\ 关于vim和commonmark:
        "\ https://github.com/preservim/vim-markdown/issues/484#issuecomment-1264229911


runtime! syntax/html.vim  " Read the HTML syntax to start with
"\ 靠它隐藏<a>XXXXXXXXXX</a>等

if exists('b:current_syntax')  | unlet b:current_syntax  | en

"\ 我改乱了这行?
if exists('b:current_syntax')  | finish  | en

" don't use standard hi link,
" it will not work with `included` syntax files
com!  -nargs=+ HiLinK
    \ hi def link <args>


" config
    syn spell toplevel
    syn case ignore
    syn sync linebreaks=1
"
    let s:conceal       = ''
    let s:concealends   = ''
    let s:__concealends = ''
    if get(g:, 'vim_markdown_conceal', 1)
        let s:conceal     = ' conceal'
        let s:concealends = ' concealends'
    en

    if get(g:, 'vim_markdown_conceal_code_blocks', 1)
        let s:__concealends = ' concealends'
    en

    let s:oneline = get(g:, 'vim_markdown_emphasis_multiline', 1)
                    \ ?   ''
                    \ : ' oneline'
                        " syn-oneline  限制在单行

" link
    " [link][]
        syn region markdownFootnotes matchgroup=markdownDelimiter start="\[^"    end="\]"
    " [link][id]
        exe  'syn region markdownID matchgroup=markdownDelimiter    start="\["    end="\]" contained oneline' . s:conceal
    " [link](URL)
        exe  'syn region markdownURL matchgroup=markdownDelimiter   start="("     end=")"  contained oneline' . s:conceal
    " ![image](URL)
        exe  'syn region markdownLink matchgroup=markdownDelimiter
                    \ start="\\\@<!!\?\[\ze[^]\n]*\n\?[^]\n]*\][[(]"
                                "\ 叹号开头
                    \ skipwhite
                    \ end="\]"
                    \ contains=@markdownNonListItem,@Spell
                    \ nextgroup=markdownURL,markdownID '..s:concealends

    " mdInlineURL  (Autolink)
        " without ¿<>¿
            syn match   mdInlineURL
            \ #https\?:\/\/\(\w\+\(:\w\+\)\?@\)\?\([A-Za-z0-9][-_0-9A-Za-z]*\.\)\{1,}\(\w\{2,}\.\?\)\{1,}\(:[0-9]\{1,5}\)\?[^] \t]*#
                       "\ protocol     optional  user:pass@  sub/domain                    .com, .co.uk, etc         optional port   path/querystring/hash fragment
                       "\ ------------ _____________________ ----------------------------- _________________________ ----------------- __

        " with ¿<>¿
            syn region mdInlineURL
                \ matchgroup=mdDelimiter
                \ start="\\\@<!<\ze[a-z][a-z0-9,.-]\{1,22}:\/\/[^> ]*>"
                      \ end=">"

        " with ¿()¿
            syn region  mdInlineURL
                \ matchgroup=mdDelimiter
                \ start="(\(https\?:\/\/\(\w\+\(:\w\+\)\?@\)\?\([A-Za-z0-9][-_0-9A-Za-z]*\.\)\{1,}\(\w\{2,}\.\?\)\{1,}\(:[0-9]\{1,5}\)\?[^] \t]*)\)\@="
                  \ end=")"

    " Link definitions: [id]: URL (Optional Title)
        syn region markdownLinkDef matchgroup=markdownDelimiter   start="^ \{,3}\zs\[\^\@!" end="]:" oneline nextgroup=markdownLinkDefTarget skipwhite
        syn region markdownLinkDefTarget start="<\?\zs\S" excludenl end="\ze[>[:space:]\n]"   contained nextgroup=markdownLinkTitle,markdownLinkDef skipwhite skipnl oneline
        syn region markdownLinkTitle matchgroup=markdownDelimiter start=+"+     end=+"+  contained
        syn region markdownLinkTitle matchgroup=markdownDelimiter start=+'+     end=+'+  contained
        syn region markdownLinkTitle matchgroup=markdownDelimiter start=+(+     end=+)+  contained


" bold etc
    "\ 为啥这样指定matchgroup?
        "\ syn region markdownBold       matchgroup=markdownBold       start="\%(\*\*\|__\)"     end="\%(\*\*\|__\)"     contains=@In_fancY
        "\ syn region markdownItalic     matchgroup=markdownItalic     start="\%(\*\|_\)"        end="\%(\*\|_\)"        contains=@In_fancY
        "\ syn region markdownBoldItalic matchgroup=markdownBoldItalic start="\%(\*\*\*\|___\)"  end="\%(\*\*\*\|___\)"  contains=@In_fancY
    syn region markdownBold        start="\%(\*\*\|__\)"     end="\%(\*\*\|__\)"     contains=@In_fancY
    syn region markdownItalic      start="\%(\*\|_\)"        end="\%(\*\|_\)"        contains=@In_fancY
    syn region markdownBoldItalic  start="\%(\*\*\*\|___\)"  end="\%(\*\*\*\|___\)"  contains=@In_fancY

    syn match  markdownDouble_staR  #\*\*#     conceal  contained containedin=markdownBold,
                                             \markdownListItem,
                                             \markdownListItemLine
    hi markdownBold gui=bold

    " Strike through
    if get(g:, 'vim_markdown_strikethrough', 0)
        exe  'syn region markdownStrike matchgroup=htmlStrike start="\%(\~\~\)" end="\%(\~\~\)"' . s:concealends
        HiLinK markdownStrike        htmlStrike
    en


    exe
    \ 'syn region htmlItalic
        \ matchgroup=markdownItalic
        \ start="\%(^\|\s\)\zs\*\ze[^\\\*\t ]\%(\%([^*]\|\\\*\|\n\)*[^\\\*\t ]\)\?\*\_W"
        \ end="[^\\\*\t ]\zs\*\ze\_W"
        \ keepend
        \ contains=@Spell'
        \ . s:oneline . s:concealends

" htmlBold  etc
    exe  'syn region htmlItalic matchgroup=markdownItalic start="\%(^\|\s\)\zs_\ze[^\\_\t ]" end="[^\\_\t ]\zs_\ze\_W" keepend contains=@Spell' . s:oneline . s:concealends
    exe  'syn region htmlBold matchgroup=markdownBold start="\%(^\|\s\)\zs\*\*\ze\S" end="\S\zs\*\*" keepend contains=@Spell' . s:oneline . s:concealends
    exe  'syn region htmlBold matchgroup=markdownBold start="\%(^\|\s\)\zs__\ze\S" end="\S\zs__" keepend contains=@Spell' . s:oneline . s:concealends
    exe  'syn region htmlBoldItalic matchgroup=markdownBoldItalic start="\%(^\|\s\)\zs\*\*\*\ze\S" end="\S\zs\*\*\*" keepend contains=@Spell' . s:oneline . s:concealends
    exe  'syn region htmlBoldItalic matchgroup=markdownBoldItalic start="\%(^\|\s\)\zs___\ze\S" end="\S\zs___" keepend contains=@Spell' . s:oneline . s:concealends


" ✌!重点!✌
"define Markdown groups
    syn match  markdownLineBreak    /  \+$/

    " markdownCode
        " 不能放在其他mdcode后面, 不然会覆盖```XXX\n```的内容
        exe    'syn region    markdownCode_InlinE'
                    \ 'matchgroup=markdownCodeEnds'
                    \ 'start=/\(\([^\\]\|^\)\\\)\@<!`/'
                    \ 'end=/`/'  . s:__concealends
                            " `inline code _hh`
            hi link markdownCode_InlinE In_BackticK

        exe    'syn region markdownCode'
                \ 'matchgroup=markdownCodeEnds'
                \ 'start=/^\s*\z(\~\{3,}\)\s*[0-9A-Za-z_+-]*\s*$/'
                \ 'end=/^\s*\z1\~*\s*$/'
                \ . s:__concealends
        exe    'syn region markdownCode      matchgroup=markdownCodeEnds    start="<pre\(\|\_s[^>]*\)\\\@<!>"                    end="</pre>"'            . s:__concealends
        exe    'syn region markdownCode      matchgroup=markdownCodeEnds    start="<code\(\|\_s[^>]*\)\\\@<!>"                   end="</code>"'           . s:__concealends
        exe    'syn region markdownCode      matchgroup=markdownCodeEnds    start=/\(\([^\\]\|^\)\\\)\@<!\~\~/                    end=/\(\([^\\]\|^\)\\\)\@<!\~\~/'        . s:__concealends


        " 4个空格以上的缩进 视为code
            " syn match  markdownCode         /^\s*\n\(\(\s\{8,}[^ ]\|\t\t\+[^\t]\).*\n\)\+/
            syn match  markdownCode         /^\s*\n\(\(\s\{4,}[^ ]\|\t\+[^\t]\).*\n\)\+/ contained
            syn match  markdownCode         /\%^\(\(\s\{4,}[^ ]\|\t\+[^\t]\).*\n\)\+/


        "\ syn match  markdownCode         #\v^\s*\n((\s{8,}[^ ]|\t\t+[^\t] ).*\n)+#
            "\ 干扰下面这个:
        "\ 我改过, concealend
            hi markdownCode_BacktickS guibg=#f5f5f5
            exe  'syn region markdownCode_BacktickS'
                    \ 'matchgroup=markdownCodeEnds'
                    \ 'start=#\v\\(([^\\]|^)\\)@<!``#'
                    \ 'skip=#\v[^`]`[^`]#'
                    \ 'end=#``#' . s:__concealends

            exe  'syn region  markdownCode_BacktickS'
                    \ 'matchgroup=markdownCodeEnds'
                    \ 'start=#\v^\s*\z(`{3,})[^`]*$#'
                    \ 'end=#\v^\s*\z1`*\s*$#' .  s:__concealends

                        "\  end的¿`¿ 可以比start多

                    "\ ``````python
                    "\ `````````````

                    "\ ```python
                    "\ ```

    " markdownRule
        syn match  markdownRule         /^\s*\*\s\{0,1}\*\s\{0,1}\*\(\*\|\s\)*$/
        syn match  markdownRule         /^\s*-\s\{0,1}-\s\{0,1}-\(-\|\s\)*$/
        syn match  markdownRule         /^\s*_\s\{0,1}_\s\{0,1}_\(_\|\s\)*$/

    syn region markdownFootnote     start="\[^"     end="\]"

    syn region markdownBlockquote   start=/^\s*>/    end=/$/ contains=markdownLink,markdownInlineURL,markdownLineBreak,@Spell


syn cluster markdownNonListItem contains=
    \ @htmlTop,
    \ htmlItalic,
    \ htmlBold,
    \ htmlBoldItalic,
    \ markdownFootnotes,
    \ markdownInlineURL,
    \ markdownLink,
    \ markdownLinkDef,
    \ markdownLineBreak,
    \ markdownBlockquote,
    \
    \ markdownCode,
    \ markdownCode_inline,
    \ markdownCode_BacktickS,
    \
    \ markdownRule,
    \ htmlH1,
    \ htmlH2,
    \ htmlH3,
    \ htmlH4,
    \ htmlH5,
    \ htmlH6,
    \ markdownMath,
    \ markdownStrike

" list/nonList
    "\ syn match  markdownList_head   #*#  contained  containedin=markdownListItem,markdownListItemLine  conceal cchar=·
    syn match  markdownList_head   #^\s*\zs\* #  contained  containedin=markdownListItem,markdownListItemLine  conceal cchar=·
    "\ syn match  markdownList_head   #^\s*\zs+ #   contained  containedin=markdownListItem,markdownListItemLine  conceal cchar=·
    "\ syn match  markdownList_head   #^\s*\zs- #   contained  containedin=markdownListItem,markdownListItemLine  conceal cchar=·

    syn match  markdownListItem     /^\s*\%([-*+]\|\d\+\.\)\ze\s\+/ contained

    syn region markdownListItemLine
        \  start="^\s*\%([-*+]\|\d\+\.\)\s\+"
          \  end="$"
        \ oneline
        \ contains=@markdownNonListItem,markdownListItem,@Spell,@In_fancY

    syn region markdownNonListItemBlock
        \ start="\(\%^\(\s*\([-*+]\|\d\+\.\)\s\+\)\@!\|\n\(\_^\_$\|\s\{4,}[^ ]\|\t+[^\t]\)\@!\)"
        \ end="^\(\s*\([-*+]\|\d\+\.\)\s\+\)\@="
        \ contains=@markdownNonListItem,@Spell,@In_fancY


"HTML headings
    " 扔掉了 matchgroup=markdownHeading
        " hi link markdownHeading HidE1
        " syn match markdownHeading '#' contained conceal cchar=-
        " markdownHeading没被定义, 默认hi def link markdownHeading Ignore?
        " markdownHeading改为其他的, 不行

    syn match SharP '#' contained conceal cchar=-
    hi SharP guibg=NONE guifg=none gui=underline

    " /home/wf/dotF/cfg/nvim/syntax/html.vim
    syn region htmlH1     start="^\s*#"           end="$" contains=Sharp,@In_fancY,markdownLink,markdownInlineURL,@Spell
    syn region htmlH2     start="^\s*##"          end="$" contains=Sharp,@In_fancY,markdownLink,markdownInlineURL,@Spell
    syn region htmlH3     start="^\s*###"         end="$" contains=Sharp,@In_fancY,markdownLink,markdownInlineURL,@Spell
    syn region htmlH4     start="^\s*####"        end="$" contains=Sharp,@In_fancY,markdownLink,markdownInlineURL,@Spell
    syn region htmlH5     start="^\s*#####"       end="$" contains=Sharp,@In_fancY,markdownLink,markdownInlineURL,@Spell
    syn region htmlH6     start="^\s*######"      end="$" contains=Sharp,@In_fancY,markdownLink,markdownInlineURL,@Spell

    syn match  htmlH1    /^.\+\n=\+$/ contains=markdownLink,markdownInlineURL,@Spell
    syn match  htmlH2    /^.\+\n-\+$/ contains=markdownLink,markdownInlineURL,@Spell


" YAML frontmatter
    if get(g:, 'vim_markdown_frontmatter', 0)
        syn include @yamlTop syntax/yaml.vim
        syn region Comment
                \ matchgroup=markdownDelimiter
                \ start="\%^---$"
                \ end="^\(---\|\.\.\.\)$"
                    \ keepend
                    \ contains=@yamlTop
        unlet! b:current_syntax
    en

    if get(g:, 'vim_markdown_toml_frontmatter', 0)
        try
            syn include @tomlTop syntax/toml.vim
            syn region Comment matchgroup=markdownDelimiter start="\%^+++$" end="^+++$" transparent contains=@tomlTop keepend
            unlet! b:current_syntax
        catch /E484/
            syn region Comment matchgroup=markdownDelimiter start="\%^+++$" end="^+++$"
        endtry
    en

    if get(g:, 'vim_markdown_json_frontmatter', 0)
        try
            syn include @jsonTop syntax/json.vim
            syn region Comment matchgroup=markdownDelimiter start="\%^{$" end="^}$" contains=@jsonTop keepend
            unlet! b:current_syntax
        catch /E484/
            syn region Comment matchgroup=markdownDelimiter start="\%^{$" end="^}$"
        endtry
    en

    if get(g:, 'vim_markdown_math', 0)
        syn include @tex syntax/tex.vim
        syn   region   markdownMath   start="\\\@<!\$"     end="\$"     skip="\\\$"   contains=@tex   keepend
        syn   region   markdownMath   start="\\\@<!\$\$"   end="\$\$"   skip="\\\$"   contains=@tex   keepend
    en


"\ 封印 conceal
    syn match markdownUnderScore '\\_'        conceal cchar=_ containedin=ALL
    syn match markdownUnderScore '\\\\'        conceal cchar=\ containedin=ALL
    syn match markdownUnderScore #\[\zs\\\[#    conceal cchar=[ containedin=ALL
    syn match markdownUnderScore #]\zs\\]#      conceal cchar=] containedin=ALL
    syn match markdownUnderScore #<br>#      conceal cchar=  containedin=ALL
                          "\ 换行符


"\ todo: 封印html的tag


" highlighting
    HiLinK markdownString        String

    HiLinK markdownCode          Ignore
    HiLinK markdownCodeStart     String
    HiLinK markdownCodeEnd       String
    HiLinK markdownCodeEnds      String

    HiLinK markdownFootnote      Comment

    HiLinK markdownBlockquote    String

    HiLinK markdownListItem      Identifier
    HiLinK markdownRule          Identifier
    HiLinK markdownLineBreak     Ignore
    "\ HiLinK markdownLineBreak     Visual
    hi markdownFootnotes gui=italic guifg=none guibg=none
    "\ HiLinK markdownFootnotes     htmlLink
    "\ see
        "\ /home/wf/dotF/cfg/nvim/syntax/html.vim
    HiLinK markdownLink          htmlLink
    HiLinK markdownURL           htmlString
    HiLinK markdownInlineURL     htmlLink
    HiLinK markdownID            Identifier
    HiLinK markdownLinkDef       markdownID
    HiLinK markdownLinkDefTarget markdownURL
    HiLinK markdownLinkTitle     htmlString
    HiLinK markdownDelimiter     Delimiter

let b:current_syntax = 'mkd'

delcommand HiLinK
